ref(mcp): Replace instance patches with middleware - #6764
ref(mcp): Replace instance patches with middleware#6764alexander-alderman-webb wants to merge 41 commits into
Conversation
Codecov Results 📊✅ 94982 passed | ⏭️ 6343 skipped | Total: 101325 | Pass Rate: 93.74% | Execution Time: 343m 9s 📊 Comparison with Base Branch
➖ Removed Tests (1)View removed tests
All tests are passing successfully. ✅ Patch coverage is 83.66%. Project has 2523 uncovered lines. Files with missing lines (1)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
- Coverage 89.88% 89.86% -0.02%
==========================================
Files 193 193 —
Lines 24832 24889 +57
Branches 8912 8954 +42
==========================================
+ Hits 22319 22366 +47
- Misses 2513 2523 +10
- Partials 1416 1440 +24Generated by Codecov Action |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 5477c8e. Configure here.
|
|
||
| uri = None | ||
| if ctx.params is not None: | ||
| uri = getattr(ctx.params, "uri", None) |
There was a problem hiding this comment.
Bug: In _instrument_v2_resource_read, getattr(ctx.params, "uri", None) is used on a dict-like object, which will always return None, making the intended URI parsing logic dead code.
Severity: LOW
Suggested Fix
Replace the incorrect attribute access with dictionary-style access. Change uri = getattr(ctx.params, "uri", None) to uri = ctx.params.get("uri") to be consistent with the access patterns used elsewhere in the middleware for the ctx.params object.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: sentry_sdk/integrations/mcp.py#L950
Potential issue: In the `_instrument_v2_resource_read` middleware, `ctx.params` is a
dictionary-like object containing raw request parameters. However, the code attempts to
retrieve the URI using `getattr(ctx.params, "uri", None)`, which is intended for objects
with attributes. This call will always return `None` because dictionaries do not have a
`.uri` attribute. As a result, the more robust URI scheme extraction logic that relies
on a parsed URI object is never executed. While a fallback mechanism using string
parsing handles most standard URIs, the intended, more reliable path is effectively dead
code. This is inconsistent with other parts of the code that correctly use
dictionary-style access like `ctx.params.get("uri")`.
There was a problem hiding this comment.
Doing a pass now to reduce dead code, thanks.

Description
Add a middleware instead of wrapping handlers on registration.
All requests go through the middleware, whereas there are multiple ways to register a handler.
Issues
Closes #6986
Reminders
uv run ruff.feat:,fix:,ref:,meta:)